package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    RoomAttrValueService roomAttrValueService;
    @Autowired
    RoomFacilityService roomFacilityService;
    @Autowired
    RoomLabelService roomLabelService;
    @Autowired
    RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    RoomLeaseTermService roomLeaseTermService;
    @Autowired
    GraphInfoService graphInfoService;

    @Autowired
    GraphInfoMapper graphInfoMapper;
    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    RoomFacilityMapper roomFacilityMapper;
    @Autowired
    RoomPaymentTypeMapper roomPaymentTypeMapper;
    @Autowired
    RoomLabelMapper roomLabelMapper;
    @Autowired
    RoomLeaseTermMapper roomLeaseTermMapper;
    @Autowired
    RoomAttrValueMapper roomAttrValueMapper;


    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {
        Long roomId = roomSubmitVo.getId();
        if (roomId != null) {
            QueryWrapper<RoomAttrValue> roomAttrValueQueryWrapper = new QueryWrapper<>();
            roomAttrValueQueryWrapper.eq("room_id", roomId);
            roomAttrValueService.remove(roomAttrValueQueryWrapper);

            QueryWrapper<RoomFacility> roomFacilityQueryWrapper = new QueryWrapper<>();
            roomFacilityQueryWrapper.eq("room_id", roomId);
            roomFacilityService.remove(roomFacilityQueryWrapper);

            QueryWrapper<RoomLabel> roomLabelQueryWrapper = new QueryWrapper<>();
            roomLabelQueryWrapper.eq("room_id", roomId);
            roomLabelService.remove(roomLabelQueryWrapper);

            QueryWrapper<RoomPaymentType> roomPaymentTypeQueryWrapper = new QueryWrapper<>();
            roomPaymentTypeQueryWrapper.eq("room_id", roomId);
            roomPaymentTypeService.remove(roomPaymentTypeQueryWrapper);

            QueryWrapper<RoomLeaseTerm> roomLeaseTermQueryWrapper = new QueryWrapper<>();
            roomLeaseTermQueryWrapper.eq("room_id", roomId);
            roomLeaseTermService.remove(roomLeaseTermQueryWrapper);

            QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
            graphInfoQueryWrapper.eq("item_type", ItemType.ROOM).eq("item_id", roomId);
            graphInfoService.remove(graphInfoQueryWrapper);

            roomSubmitVo.setUpdateTime(null);
        }

        this.saveOrUpdate(roomSubmitVo);
        Long roomSubmitVoId = roomSubmitVo.getId();

        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (!attrValueIds.isEmpty()) {
            List<RoomAttrValue> roomAttrValueList = new ArrayList<>();
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder()
                        .attrValueId(attrValueId).roomId(roomSubmitVoId).build();
                roomAttrValueList.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValueList);
        }

        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (!facilityInfoIds.isEmpty()) {
            List<RoomFacility> roomFacilityList = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility roomFacility = RoomFacility.builder()
                        .facilityId(facilityInfoId).roomId(roomSubmitVoId).build();
                roomFacilityList.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilityList);
        }

        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (!labelInfoIds.isEmpty()) {
            List<RoomLabel> roomLabelList = new ArrayList<>();
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabel = RoomLabel.builder()
                        .labelId(labelInfoId).roomId(roomSubmitVoId).build();
                roomLabelList.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabelList);
        }

        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!paymentTypeIds.isEmpty()) {
            List<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType = RoomPaymentType.builder()
                        .paymentTypeId(paymentTypeId).roomId(roomSubmitVoId).build();
                roomPaymentTypeList.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }

        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!leaseTermIds.isEmpty()) {
            List<RoomLeaseTerm> roomLeaseTermList = new ArrayList<>();
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder()
                        .leaseTermId(leaseTermId).roomId(roomSubmitVoId).build();
                roomLeaseTermList.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTermList);
        }

        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (!graphVoList.isEmpty()) {
            List<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(roomSubmitVoId);
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }
    }

    @Override
    public IPage<RoomItemVo> pageItem(IPage<RoomInfo> roomInfoIPage, RoomQueryVo roomQueryVo) {

        IPage<RoomItemVo> roomItemVoIPage = baseMapper.selectPageItem(roomInfoIPage, roomQueryVo);
        return roomItemVoIPage;
    }

    @Override
    public RoomDetailVo getDetaliById(Long id) {
        RoomInfo roomInfo = baseMapper.selectById(id);

        //查图片
        QueryWrapper<GraphInfo> graphInfoQueryWrapperraphInfo = new QueryWrapper<GraphInfo>()
                .eq("item_type", 2).eq("item_id", id);
        List<GraphInfo> graphInfos = graphInfoMapper.selectList(graphInfoQueryWrapperraphInfo);
        List<GraphVo> graphVos = graphInfos.stream().map(g -> {
            GraphVo graphVo = new GraphVo();
            graphVo.setName(g.getName());
            graphVo.setUrl(g.getUrl());
            return graphVo;
        }).collect(Collectors.toList());

//公寓信息
        QueryWrapper<ApartmentInfo> apartmentInfoQueryWrapper = new QueryWrapper<>();
        apartmentInfoQueryWrapper.eq("id", roomInfo.getApartmentId());
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectOne(apartmentInfoQueryWrapper);

        //标签信息
        List<LabelInfo> labelInfoList = roomLabelMapper.selectByRoomId(id);
        //属性
        List<FacilityInfo> facilityInfoList = roomFacilityMapper.selectListByRoomId(id);
        //配套
        List<AttrValueVo> attrValueVoList = roomAttrValueMapper.selectListByRoomId(id);
        //租期
        List<LeaseTerm> leaseTermList = roomLeaseTermMapper.selectListByRoomId(id);

        //支付方式
        List<PaymentType> paymentTypeList = roomPaymentTypeMapper.selectListByRoomId(id);


        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        roomDetailVo.setGraphVoList(graphVos);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        return roomDetailVo;
    }

    @Override
    public void removeRoomById(Long id) {
// 房间配套
        QueryWrapper<RoomFacility> roomFacilityQueryWrapper = new QueryWrapper<>();
        roomFacilityQueryWrapper.eq("room_id",id);
        roomFacilityMapper.delete(roomFacilityQueryWrapper);

        // 房间属性
        QueryWrapper<RoomAttrValue> roomAttrValueQueryWrapper = new QueryWrapper<>();
        roomAttrValueQueryWrapper.eq("room_id",id);
        roomAttrValueMapper.delete(roomAttrValueQueryWrapper);

        // 房间标签
        QueryWrapper<RoomLabel> roomLabelQueryWrapper = new QueryWrapper<>();
        roomLabelQueryWrapper.eq("room_id",id);
        roomLabelMapper.delete(roomLabelQueryWrapper);

        // 房间租期
        QueryWrapper<RoomLeaseTerm> roomLeaseTermQueryWrapper = new QueryWrapper<>();
        roomLeaseTermQueryWrapper.eq("room_id",id);
        roomLeaseTermMapper.delete(roomLeaseTermQueryWrapper);

        // 房间支付
        QueryWrapper<RoomPaymentType> roomPaymentTypeQueryWrapper = new QueryWrapper<>();
        roomPaymentTypeQueryWrapper.eq("room_id",id);
        roomPaymentTypeMapper.delete(roomPaymentTypeQueryWrapper);

        // 图片
        QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
        graphInfoQueryWrapper.eq("item_type",2);
        graphInfoQueryWrapper.eq("item_id",id);
        graphInfoMapper.delete(graphInfoQueryWrapper);

        // 房间数据
        baseMapper.deleteById(id);

    }
}




